package com.steve.summary.seata.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.steve.summary.seata.entity.Account;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;

/**
 * @Author: STEVE
 * @Description: 账户数据访问层
 * @since: 2025-07-08
 */
@Mapper
public interface AccountMapper extends BaseMapper<Account> {

    /**
     * 根据用户id查询
     *
     * @param userId 用户id
     * @return
     */
    @Select("SELECT * FROM account WHERE user_id = #{userId}")
    Account selectByUserId(@Param("userId") String userId);

    /**
     * 扣减余额
     *
     * @param userId 用户id
     * @param money  金额
     * @return
     */
    @Update("UPDATE account SET balance = balance - #{money} WHERE user_id = #{userId} AND balance >= #{money}")
    int deductBalance(@Param("userId") String userId, @Param("money") BigDecimal money);

    /**
     * 恢复余额
     *
     * @param userId 用户id
     * @param money  金额
     * @return
     */
    @Update("UPDATE account SET balance = balance + #{money} WHERE user_id = #{userId}")
    int restoreBalance(@Param("userId") String userId, @Param("money") BigDecimal money);

    /**
     * 冻结余额
     *
     * @param userId 用户id
     * @param money  金额
     * @return
     */
    int freezeBalance(@Param("userId") String userId, @Param("money") BigDecimal money);

}
